<link rel="stylesheet" href="assets/user/css/goods.css">
<div class="layui-container">
    <div class="layui-row">
        <div class="layui-col-sm12 layui-col-md12 layui-col-lg12">
			<div class="widget-head">
				<div class="widget-title layer-cf">添加商品</div>
			</div>
			<form id="my-form" class="layer-form tpl-form-line-form" method="post">
				<div class="widget-body layui-col-lg12">
					<div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
						<ul class="layui-tab-title">
							<li class="layui-this">基础设置</li>
							<li>商品相册</li>
							<li>商品详情</li>
						</ul>
						<div class="layui-tab-content">
							<div class="layui-tab-item layui-show">
								<div class="layer-form-group">
									<label class="layui-form-label form-require">商品名称：</label>
									<div class="layui-col-sm7  layer-midd-left">
										<input type="text" class="tpl-form-input" name="goods_name" value="{$goodsInfo['goods_name'] ?? ''}" required>
									</div>
								</div>
								<div class="layer-form-group">
									<label class="layui-form-label form-require">商品简介：</label>
									<div class="layui-col-sm7  layer-midd-left">
										  <textarea rows="3" cols="50" name="goods_remark">{$goodsInfo['goods_remark']??''}</textarea>
									</div>
								</div>
								<div class="layer-form-group">
									<label class="layui-form-label form-require">商品库存：</label>
									<div class="layui-col-sm7  layer-midd-left">
										<input type="text" id="store_count"  name="store_count" value="{$goodsInfo['store_count'] ?? ''}"  lay-filter="tree" class="layui-input">
									</div>
								</div>
								<div class="layer-form-group">
									<label class="layui-form-label form-require">商品单价：</label>
									<div class="layui-col-sm7  layer-midd-left">
										<input type="text" id="good_mone"  name="good_mone" value="{$goodsInfo['good_mone'] ?? ''}"  lay-filter="tree" class="layui-input">
									</div>
								</div>
								<div class="layer-form-group">
									<label class="layui-form-label form-require">商品品牌：</label>
									<div class="layui-col-sm7  layer-midd-left">
										<input type="text" id="good_pinpai"  name="good_pinpai" value="{$goodsInfo['good_pinpai'] ?? ''}"  lay-filter="tree" class="layui-input">
									</div>
								</div>
								<div class="layer-form-group">
									<label class="layui-form-label form-require">商品归属：</label>
									<div class="layui-col-sm2 layer-midd-left">
										<select id="provinceid" name="province_id" lay-verify="required" onchange="sheng()">
											<option value="">全部</option>
											{foreach $province as $val}
											{if $val['province_id'] == $goodsInfo['province_id']}
											<option value="{$val['province_id']}" selected = "selected">{$val['name']}</option>
											{else/}
											<option value="{$val['province_id']}">{$val['name']}</option>
											{/if}
											{/foreach}

										</select>
									</div>

									<div class="layui-col-sm2 layer-midd-left">
										<select id="cityid" name="city_id" lay-verify="required" onchange="xian()">
											{if !empty($citylist)}
											{foreach $citylist as $c}
											{if $c['city_id'] == $goodsInfo['city_id']}
											<option value="{$c['city_id']}" selected = "selected">{$c['name']}</option>
											{else/}
											<option value="{$c['city_id']}">{$c['name']}</option>
											{/if}
											{/foreach}
											{/if}
										</select>
									</div>

									<div class="layui-col-sm2 layer-midd-left">
										<select id="districtid" name="district_id" lay-verify="required">
											{if !empty($dislist)}
											{foreach $dislist as $a}
											{if $a['district_id'] == $goodsInfo['district_id']}
											<option value="{$a['district_id']}" selected = "selected">{$a['name']}</option>
											{else/}
											<option value="{$a['district_id']}">{$a['name']}</option>
											{/if}
											{/foreach}
											{/if}
										</select>
									</div>
								</div>
								<div class="layer-form-group">
									<label class="layui-form-label form-require">商品规格：</label>
									<div class="layui-col-sm7  layer-midd-left">
										<input type="text" id="spec_type"  name="spec_type" value="{$goodsInfo['spec_type'] ?? ''}"  lay-filter="tree" class="layui-input">
									</div>
								</div>
								<div class="layer-form-group">
									<label class="layui-form-label form-require">单位：</label>
									<div class="layui-col-sm7  layer-midd-left">
										<input type="text" id="weight"  name="weight" value="{$goodsInfo['weight'] ?? ''}"  lay-filter="tree" class="layui-input">
									</div>
								</div>
							</div> 
					
							<div class="layui-tab-item">

								<div class="layer-form-group">
									<label class="layui-form-label form-require">商品主图：</label>
									<div class="layui-col-sm7  layer-midd-left">
										<img src="/uploads/{$goodsInfo['good_image'] ?? ''}.jpg" id="images" width="100">
										<input type="hidden" id="gooimage"  name="gooimage" value="{$goodsInfo['good_image'] ?? ''}">
										<input type="file" id="goods_images"  name="goods_images" value=""onchange="aaaa()">
									</div>
								</div>



								<div class="layer-form-group">
									<label class="layui-form-label form-require">商品图片： </label>
									<div class="layui-col-sm7  layer-midd-left">
										<div class="layer-form-file">
											<div class="layer-form-file">
												<input type="file" id="good_arr"  name="good_arr" value=""onchange="bbb()">
												<div class="uploader-list layer-cf">
													{if isset($arr_imge)}
													{foreach $arr_imge as $image}
														<div class="file-item" id="shanchu1">
																<img src="/uploads/{$image['image_id']}.jpg">
															<input type="hidden"
																   name="good_image[]"
																   value="{$image['image_id']??''}">
															<i class="mdi menu-icon mdi-window-close file-item-delete" onclick="cccc(shanchu1)"></i>
														</div>
												{/foreach}
												{/if}
												</div>
											</div>
											<div class="help-block">

											</div>
										</div>
									</div>
								</div>
							</div>
							<div class="layui-tab-item">
								 
								<div class="layer-form-group">
									<label class="layui-form-label form-require">商品详情： </label>
									<div class="layui-col-sm7  layer-midd-left">
									
										<textarea id="container" name="goods_content" type="text/plain">
										{$goodsInfo['goods_content']}
										</textarea>
									</div>
								</div>
						
							</div>

							<div class="layui-tab-item">
								<table class="table table-bordered attr" id="goods_attr_table" style="width:80%;">

								</table>
							</div>
						</div>
					</div>
					<div class="layer-form-group">
						<div class="layui-col-sm7  layer-u-sm-push-3 layer-margin-top-lg">
							 <input type="hidden"  class="goodsid" name="goods_id" value="{$goodsInfo.goods_id}">
							 <input type="hidden"  value="{$goodsInfo['cat_id']??''}"  lay-filter="tree" class="layui-input catid">
							<button type="submit" class="j-submit layui-btn layer-btn-secondary">提交
							</button>
						</div>
					</div>
				</div>
            </form>
        </div>
    </div>
</div>
<!-- 图片文件列表模板 -->

<!-- 文件库弹窗 -->
{include file="layouts/_template/file_library" /}
{include file="item/_template/spec_many" /}
<script src="assets/user/js/goods.spec.js"></script>
<script>
var cat_id="{$goodsInfo['cat_id']??''}";
</script>
<script src="assets/user/js/goods.category.js"></script>
<script>
		$(function () {
			  // 富文本编辑器
		layui.use('layedit', function(){
		var layedit = layui.layedit;
		layedit.build('container', {
		height: 600, //设置编辑器高度
		uploadImage: {
                    url: '/index.php?s=/user/upload/upload' //接口url
                  , type: 'post' //默认post
                }
		});	
		});
		
        // 选择图片
        $('.j-withdraw_apply').selectImages({
            name: 'goods[goods_images][]'
            , multiple: true
        });
		 //地址的定位
		layui.use('element', function(){
			var $ = layui.jquery
			,element = layui.element; //Tab的切换功能，切换事件监听等，需要依赖element模块
			
		});
        /**
         * 表单验证提交
         * @type {*}
         */
        $('#my-form').superForm();
    });
	
</script>

<script>
    function  sheng() {
        var provinceid = $("#provinceid").val();
        $.ajax({
            url: "{:url('Item/cityList')}",
            type: 'post',
            dataType: 'json',
            data: {provinceid:provinceid},
            success: function (res) {
                var data = res.data;
                $("#cityid").html('<option value="">全部</option>');
                $("#districtid").html('');
                if (res.code === 200) {
                    for (i=0;i<data.length;i++){
                        var html = '<option value="'+data[i].city_id+'">'+data[i].name+'</option>';
                        $("#cityid").append(html);
                    }
                } else {
                    layer.msg('添加失败', {icon: 2});
                }
            }
        });
        //alert(provinceid);
    }

    function  xian() {
        var cityid = $("#cityid").val();
        $.ajax({
            url: "{:url('Item/districtList')}",
            type: 'post',
            dataType: 'json',
            data: {cityid:cityid},
            success: function (res) {
                var data = res.data;
                $("#districtid").html('<option value="">全部</option>');
                if (res.code === 200) {
                    for (i=0;i<data.length;i++){
                        var html = '<option value="'+data[i].district_id+'">'+data[i].name+'</option>';
                        $("#districtid").append(html);
                    }
                } else {
                    layer.msg('添加失败', {icon: 2});
                }
            }
        });
        //alert(provinceid);
    }
    function aaaa(){
        var goods_images = $('#goods_images');
        var formData = new FormData();
        formData.append('file', $('#goods_images')[0].files[0]); // 固定格式
        $.ajax({
            url: "{:url('Images/images')}",
            type: 'post',
            dataType: 'json',
            data: formData,
            processData:false,
            contentType:false,
            success: function (res) {
                if (res.code === 1000) {
                    $("#images").attr("src",'/uploads/'+res.data+'.jpg');
                    $("#gooimage").val(res.data);
                } else {
                    layer.msg('添加失败', {icon: 2});
                }
            }
        });
	}

    function bbb(){

        var good_arr = $('#good_arr');
        var formData = new FormData();
        formData.append('file', $('#good_arr')[0].files[0]); // 固定格式
        $.ajax({
            url: "{:url('Images/images')}",
            type: 'post',
            dataType: 'json',
            data: formData,
            processData:false,
            contentType:false,
            success: function (res) {
                if (res.code === 1000) {
					var html = '<div class="file-item" id="shanchu1"><img src="/uploads/'+res.data+'.jpg"><input type="hidden" name="good_image[]" value="'+res.data+'"> <i class="mdi menu-icon mdi-window-close file-item-delete delImg"></i></div>'
                    $(".uploader-list").append(html);
                } else {
                    layer.msg('添加失败');
                }
            }
        });
    }
    $(".uploader-list").on('click',".delImg",function () {
        $(this).parent().remove();
    })
</script>